9 namespace Core.TypeCast
12 using System.Reflection;
14 using System.Runtime.CompilerServices;
20 public static partial class ObjectExtension
34 [MethodImpl(MethodImplOptions.AggressiveInlining)]
35 public static TOut CastTo<TOut>(
this object self, TOut defaultValue =
default(TOut))
40 self.TryCast<object, TOut>(out result, defaultValue, throwException: defaultValue?.IsDefaultValue() ==
true, unboxObjectType:
true);
44 [MethodImpl(MethodImplOptions.AggressiveInlining)]
45 public static TOut CastTo<TOut>(
this object self)
48 self.TryCast<object, TOut>(out result,
default(TOut), throwException:
false, unboxObjectType:
true);
75 [MethodImpl(MethodImplOptions.AggressiveInlining)]
76 public static TOut CastTo<TIn, TOut>(
this TIn?
self, TOut defaultValue =
default(TOut),
bool withContext =
false) where TIn :
struct 79 self.TryCast<object, TOut>(out result, defaultValue:
default(TOut), throwException:
false, unboxObjectType:
false, contextInstance: (withContext ?
new ConvertContext(
default(TOut)) { Nullable =
true} : null));
108 public static TOut CastTo<TIn, TOut>(
this TIn
self, TOut defaultValue =
default(TOut))
113 if(ConverterCollection.Initialized ==
true 114 && ConverterCollection.CurrentInstance?.Settings.AllowGenericTypes ==
false 115 && typeof(TIn).IsConstructedGenericType ==
true)
117 throw new ConverterException(
ConverterCause.ConverterArgumentGenericType);
120 self.TryCast<TIn, TOut>(out result, defaultValue, throwException:
true);
150 [MethodImpl(MethodImplOptions.AggressiveInlining)]
151 public static object CastTo(
this object self, Type typeTo,
object defaultValue = null,
bool unboxObjectType =
true)
154 self.TryCast(typeTo, out result, defaultValue, throwException:
false, unboxObjectType: unboxObjectType);
191 public static bool TryCast<TIn, TOut>(
this TIn
self, out TOut result, TOut defaultValue =
default(TOut),
bool throwException =
false,
bool unboxObjectType =
false, IConvertContext contextInstance = null)
194 var typeArgument = (defaultValue == null || defaultValue.IsDefaultValue() ==
true) ? null : typeof(TOut);
195 if(GetConverterOrDefault(
self, out converter, out result, typeArgument: typeArgument, throwException: throwException, unboxObjectType: unboxObjectType))
200 return InvokeConvert(
self, out result, defaultValue, throwException, converter, contextInstance: contextInstance);
224 public static bool TryCast(
this object self, Type typeTo, out
object result,
object defaultValue,
bool throwException =
false,
bool unboxObjectType =
true, IConvertContext contextInstance = null)
226 Type typeFrom = null;
227 Type typeArgument = null;
228 if(unboxObjectType ==
true)
230 typeFrom =
self.GetType();
231 if(typeFrom == typeTo)
233 result = (object)
self;
237 typeArgument = defaultValue == null ? null : defaultValue.GetType();
241 if(GetConverterOrDefault(
self, out converter, out result, typeArgument: typeArgument, typeTo: typeTo, throwException: throwException, unboxObjectType:
true))
246 if(defaultValue == null)
248 defaultValue = typeTo.GetTypeInfo().GetDefault();
251 return InvokeConvert(
self, out result, defaultValue, throwException, converter, contextInstance: contextInstance);
ConverterCause
Contains the reasons for a ConverterException to be raised.